﻿using System;
using System.Collections;

namespace YArchitech.HCLoad.XmlData
{
	public class FloorCollect : Hashtable
	{
		public FloorCollect(Building building)
		{
			this.parent = building;
			this.AddFloor();
		}

		public Building Parent
		{
			get
			{
				return this.parent;
			}
			set
			{
				this.parent = value;
			}
		}

		private void AddFloor()
		{
			try
			{
				Floor floor = new Floor(this);
				int num = this.parent.Parent.Parent.Parent.AddCurrID();
				floor.ID = num;
				this.Add(num, floor);
				this.parent.Parent.Parent.Parent.hCurrIDList.Add(num, floor);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}

		public int AddFloor(Floor floor)
		{
			int result;
			try
			{
				floor.Parent = this;
				int num = this.parent.Parent.Parent.Parent.AddCurrID();
				floor.ID = num;
				this.Add(num, floor);
				this.parent.Parent.Parent.Parent.hCurrIDList.Add(num, floor);
				result = num;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		public void RemoveFloor(int ID)
		{
			try
			{
				this.Remove(ID);
				this.parent.Parent.Parent.Parent.hCurrIDList.Remove(ID);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}

		public void ClearFloor()
		{
			try
			{
				foreach (object obj in this)
				{
					DictionaryEntry dictionaryEntry = (DictionaryEntry)obj;
					this.parent.Parent.Parent.Parent.hCurrIDList.Remove(dictionaryEntry.Key);
				}
				this.Clear();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}

		public Floor GetFloor(int ID)
		{
			Floor result;
			try
			{
				result = (Floor)this[ID];
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		public bool LoadFloor(Floor floor)
		{
			bool result;
			try
			{
				floor.Parent = this;
				int id = floor.ID;
				if (this.parent.Parent.Parent.Parent.hCurrIDList.ContainsKey(id))
				{
					result = false;
				}
				else
				{
					if (id > this.parent.Parent.Parent.Parent.CurrID)
					{
						this.parent.Parent.Parent.Parent.CurrID = id;
					}
					this.Add(id, floor);
					this.parent.Parent.Parent.Parent.hCurrIDList.Add(id, floor);
					result = true;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		private Building parent;
	}
}
